/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __pc_cycle_h__
#define __pc_cycle_h__

#include <QObject>
#include <QCoreApplication>
#include <QMap>
#include <QList>

#include <procycle/macrotask.h>
#include <procycle/file_storable.h>


namespace PC {


const QString CYCLE_ELEMENT_NAME = "cycle";


class Cycle : public MacroTask, public FileStorable {

	Q_OBJECT

	friend class Task;
	friend class AtomTask;
	friend class MacroTask;
	friend class Link;

public:



	enum Property {
		PROP_DRAWCRITICAL,
		PROP_DRAWOVERLAP,
		PROP_DRAWFLOATS,
		PROP_DRAWTEXT,
	};





	Cycle();
	virtual ~Cycle();



	virtual QString tbsId() const {
		// TODO: implement through project
		return "0";
	}

	Task* resolvAbsoluteTask(QString tbsId);

	virtual void preSchedule();

	virtual void schedule();

	virtual void postSchedule();

	Link::const_iterator linkBegin() const {
		return m_links.begin();
	}
	Link::iterator linkBegin() {
		return m_links.begin();
	}
	Link::const_iterator linkEnd() const {
		return m_links.end();
	}
	Link::iterator linkEnd() {
		return m_links.end();
	}

	Link* createLink(Task* from, Task* to, Link::Type type=Link::TYPE_FS, int lag=0);

	void deleteLink(PC::Link* link);

	virtual QString elementName() const {
		return CYCLE_ELEMENT_NAME;
	}
	virtual void loadFromElement (const QDomElement& element);
	virtual QDomElement saveToElement (QDomDocument& doc) const;


signals:
	void scheduled();

	void linkChanged(PC::Link* link);
	void linkCreated(PC::Link* link);
	void linkWillDelete(PC::Link* link);


protected:

	void connectTask(Task* task);
	void disconnectTask(Task* task);
	void connectLink(Link* link);
	void disconnectLink(Link* link);

	QList<Link*>			m_links;
};

}

#endif /* __pc_cycle_h__ */
